//
//  PushCoinViewController.m
//  AppleMarket
//
//  Created by yangy on 02/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "PushCoinViewController.h"
// controller
#import "PushRecordViewController.h"
#import "CoinListViewController.h"
// model
#import "CoinAssetModel.h"

@interface PushCoinViewController ()
@property (nonatomic, strong) UIButton *coinNameButton;     // 当前币种名称
@property (nonatomic, strong) UIImageView *QRcodeImageView; // 二维码
@property (nonatomic, strong) UIButton *saveButton;  // 保存二维码
@property (nonatomic, strong) UILabel *addressLabel; // 充值地址
@property (nonatomic, strong) UIButton *copyAddrButton;  // 复制按钮
@property (nonatomic, strong) UILabel *explainLabel; // 说明
@property (nonatomic, strong) UIImageView *directView;
@property (nonatomic, strong) CoinAssetModel *curModel;
@end

@implementation PushCoinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"币币账户充值";
    self.view.backgroundColor = [UIColor whiteColor];
    self.curModel = [self.coinModels firstObject];
    [self setNagivationBar];
    [self initView];
    [self update];
}

- (void) setNagivationBar
{
    UIButton *rightButton = [UIButton cm_ButtonWithTitle:@"充值记录" font:kFont(14) titleColor:kColorWithHex(0x80878E)];
    rightButton.frame = CGRectMake(0, 0, 88, 44);
    rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 16,0, 0);
    [rightButton setEnlargeEdgeWithTop:0 right:10 bottom:10 left:40];
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        PushRecordViewController *recordVC = [PushRecordViewController new];
        recordVC.coinModel = self.curModel;
        [self bh_PushViewController:recordVC hiddenTabbar:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void) initView
{
    [self.view addSubview:self.coinNameButton];
    [self.view addSubview:self.directView];
    [self.view addSubview:self.QRcodeImageView];
    [self.view addSubview:self.saveButton];
    [self.view addSubview:self.addressLabel];
    [self.view addSubview:self.copyAddrButton];
    [self.view addSubview:self.explainLabel];
    [self setSubviewLayout];
}

- (void) setSubviewLayout
{
    [_coinNameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
        make.height.mas_equalTo(65);
    }];
    
    [_directView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.coinNameButton.mas_centerY);
    }];
    
    [_QRcodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(153, 153));
        make.top.mas_equalTo(self.coinNameButton.mas_bottom).offset(47);
    }];
    
    [_saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.QRcodeImageView.mas_bottom).offset(18);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.saveButton.mas_bottom).offset(55);
    }];
    
    [_copyAddrButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.addressLabel.mas_bottom).offset(21);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [_explainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(15);
        make.right.mas_equalTo(self.view.mas_right).offset(-15);
        make.top.mas_equalTo(self.copyAddrButton.mas_bottom).offset(42);
    }];
}

- (void) update
{
    self.saveButton.hidden = self.addressLabel.hidden = self.copyAddrButton.hidden = self.explainLabel.hidden = !self.curModel || !self.curModel.BlockAddr? YES : NO;
    if(!self.curModel){
        [self.coinNameButton setTitle:@"请选择币种" forState:UIControlStateNormal];
    }
    
    // 币种名称
    [self.coinNameButton setTitle:self.curModel.Unit forState:UIControlStateNormal];
    // 充值二维码
    self.QRcodeImageView.image = [UtilsCommon creatQRcodeWithUrlstring:self.curModel.BlockAddr imageSize:CGSizeMake(153, 153)];
    // 充值地址
    self.addressLabel.text = self.curModel.BlockAddr;
    // 充值说明
    
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[self.curModel.DepositTips dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    self.explainLabel.attributedText = attrStr;
}

- (UIButton *)coinNameButton
{
    if(!_coinNameButton){
        _coinNameButton = [UIButton cm_ButtonWithTitle:@"选择币种" font:kFont(19) titleColor:kColorWithHex(0x0E304C) bgColor:kColorWithHex(0xF7FBFC)];
        [_coinNameButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
        [_coinNameButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_coinNameButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
        @weakify(self);
        [[_coinNameButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            // 选择币种
            CoinListViewController *listVC = [[CoinListViewController alloc] init];
            listVC.coinModels = self.coinModels;
            listVC.selectModel = self.curModel;
            listVC.SelectedHandler = ^(CoinAssetModel * _Nonnull model) {
                self.curModel = model;
                [self update];
            };
            [self.navigationController pushViewController:listVC animated:YES];
        }];
    }
    return _coinNameButton;
}

- (UIImageView *)directView
{
    if(!_directView){
        _directView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"more"]];
    }
    return _directView;
}

- (UIImageView *)QRcodeImageView
{
    if(!_QRcodeImageView){
        _QRcodeImageView = [[UIImageView alloc] init];
    }
    return _QRcodeImageView;
}

- (UIButton *)saveButton
{
    if(!_saveButton){
        _saveButton = [UIButton cm_ButtonWithTitle:@"保存二维码图片" font:kFont(15) titleColor:kColorWithHex(0x3291EB)];
        @weakify(self);
        [[_saveButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
           UIImage *image = self.QRcodeImageView.image;
            if(image){
                UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
                [SVProgressHUD showInfoWithStatus:@"保存成功"];
            }else{
                [SVProgressHUD showInfoWithStatus:@"保存失败"];
            }
        }];
    }
    return _saveButton;
}

- (UILabel *)addressLabel
{
    if(!_addressLabel){
        _addressLabel = [UILabel cm_labelWithText:@"" font:kFont(14) textColor:kColorWithHex(0x50565C)];
    }
    return _addressLabel;
}

- (UIButton *)copyAddrButton
{
    if(!_copyAddrButton){
        _copyAddrButton = [UIButton cm_ButtonWithTitle:@"复制地址" font:kFont(15) titleColor:kColorWithHex(0x3291EB)];
        @weakify(self);
        [[_copyAddrButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if(self.curModel && !kStringIsEmpty(self.curModel.BlockAddr)){
                [UIPasteboard generalPasteboard].string = self.curModel.BlockAddr;
                [SVProgressHUD showInfoWithStatus:@"复制成功"];
            }else{
                [SVProgressHUD showInfoWithStatus:@"复制失败"];
            }
        }];
    }
    return _copyAddrButton;
}

- (UILabel *)explainLabel
{
    if(!_explainLabel){
        _explainLabel = [UILabel cm_labelWithText:@"" font:kFont(12) textColor:kColorWithHex(0xB5C0CB)];
        _explainLabel.numberOfLines = 0;
        _explainLabel.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _explainLabel;
}

@end
